{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "307926af",
   "metadata": {},
   "source": [
    "## 本周学习内容\n",
    "\n",
    "### 切片\n",
    "\n",
    "> **切片 (slice) 是对序列型对象 (如 list, string, tuple)的一种高级索引方法。 普通索引只取出序列中 一个下标 对应的元素，而切片取出序列中 一个范围 对应的元素，这里的范围不是狭义上的连续片段。**\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0990ef25",
   "metadata": {},
   "source": [
    "* 1.1简单切片\n",
    "```\n",
    "a[start:stop]\n",
    "```\n",
    "* 例1\n",
    "\n",
    "```\n",
    " >>> a\n",
    " [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    " >>> a[2:3]\n",
    " [2]\n",
    " >>> a[5:9]\n",
    " [5, 6, 7, 8]\n",
    " >>> a[5:-1]\n",
    " [5, 6, 7, 8]\n",
    " ```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48b0d756",
   "metadata": {},
   "source": [
    "* 1.1.1 超出有效索引范围\n",
    "\n",
    ">   当start或stop超出上文提到的有效索引范围​时，切片操作不会抛出异常，而是进行截断。\n",
    "\n",
    "* 例2\n",
    "```\n",
    " >>> a\n",
    " [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    " >>> a[-100:5]\n",
    " [0, 1, 2, 3, 4]\n",
    " >>> a[5:100]\n",
    " [5, 6, 7, 8, 9]\n",
    " >>> a[-100:100]\n",
    " [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    " ```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f7953ea",
   "metadata": {},
   "source": [
    "* 1.2 扩展切片\n",
    "```\n",
    "a[start:stop:step]\n",
    "```\n",
    "\n",
    "> 其中step是一个非零整数，即比简单切片多了调整步长的功能\n",
    "\n",
    "> 此时切片的行为可概括为：从start对应的位置出发，以step为步长索引序列，直至越过stop对应的位置，且不包括stop本身\n",
    "\n",
    "* 1.2.1 step为正数，与简单切片完全一致\n",
    "> 如下\n",
    "\n",
    "```\n",
    " >>> a\n",
    " [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    " >>> a[0:6:2]\n",
    " [0, 2, 4]\n",
    " \n",
    "```\n",
    "\n",
    "* 1.2.2 step为负数:当step为负数时，切片将其解释为从start出发以步长|step|逆序索引序列\n",
    "```\n",
    ">>> a\n",
    " [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    " >>> a[5::-1]             # step=-1，从右往左取值，从start=6开始，一直取到“起点”0。\n",
    " [5, 4, 3, 2, 1, 0]\n",
    " >>> a[:4:-2]\n",
    " [9, 7, 5]\n",
    " ```\n",
    " * 1.2.3start & step 全为负\n",
    " ```\n",
    " >>>a[1:6]\n",
    ">>> [1, 2, 3, 4, 5]   #step=1，从左往右取值，start=1到end=6同样表示从左往右取值。\n",
    "```\n",
    "\n",
    "* 1.3 常见切片操作\n",
    "* 1.3.1 取偶数\n",
    "```\n",
    ">>>b = a[::2]\n",
    "[0, 2, 4, 6, 8]\n",
    "```\n",
    "* 1.3.2 取奇数\n",
    "```\n",
    ">>>b = a[1::2]\n",
    "[1, 3, 5, 7, 9]\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1d69802b",
   "metadata": {},
   "source": [
    "* 1.2 实验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d85b36ce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['A', 'B', 'C', 'D']\n",
      "A C\n",
      "['B', 'C']\n",
      "D\n",
      "['B', 'C']\n",
      "['C', 'D']\n",
      "['A', 'B']\n"
     ]
    }
   ],
   "source": [
    "# 例子\n",
    "\n",
    "names = [\"A\",\"B\",\"C\",\"D\"]\n",
    "\n",
    "print(names)\n",
    "\n",
    "print(names[0],names[2]) \n",
    "#取出某一个值\n",
    "print(names[1:3]) \n",
    "#切片 取出中间一些连续的值（还是列表）\n",
    "                            #思考：如果列表有几千个值，几万个值，那么想取出最后一个值怎么办？\n",
    "print(names[-1])   \n",
    "#切片 取出最后一个值\n",
    "print(names[-3:-1])  \n",
    "#切片 从后往前取出一些值（还是列表），思考为什么不是[-1:-3]?\n",
    "print(names[-2:])  \n",
    "#切片 取到最后一个值\n",
    "print(names[:2])  \n",
    "#切片 同上，即“0”可以省略"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "332feab4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# 书本练习\n",
    "\n",
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)  # list() 可以将 str  to(转)  list\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "\n",
    "on_str = ''.join(plist[1:3])\n",
    "print(on_str)\n",
    "new_phrase = on_str+ ''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "print(plist)\n",
    "print(new_phrase)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
